package com.application.tourapp;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.tourapp.dal.FavoritesDAL;
import com.application.tourapp.dal.TourItemDAL;
import com.application.tourapp.global.VaraibleGlobal;
import com.application.tourapp.model.Item;

/**
 * La classe qui resprente les details d'une activite
 * 
 * @author Ramy
 *
 */

public class ItemDetailsActivity extends GenericActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item_details);
		setHeader(getResources().getString(R.string.Det), true, true);
		Intent intent = getIntent();
		String itemName = intent.getStringExtra("name_item");
		String itemDescription = intent.getStringExtra("item_description");
		final String itemPhoneNumber = intent.getStringExtra("item_number");
		final String itemWebsite = intent.getStringExtra("item_site");
		final String itemLng = intent.getStringExtra("item_lng");
		final String itemLat = intent.getStringExtra("item_lat");
		final String itemId = intent.getStringExtra("item_id");

		TextView itemTitleView = (TextView) findViewById(R.id.itemTitle);
		TextView itemDescriptionView = (TextView) findViewById(R.id.itemDescription);
		Button buttonSite = (Button) findViewById(R.id.buttonSite);
		Button buttonAppler = (Button) findViewById(R.id.buttonAppler);
		Button buttonMap = (Button) findViewById(R.id.buttonMap);
		final Button buttonFav = (Button) findViewById(R.id.buttonFav);
		ImageView imageItem = (ImageView) findViewById(R.id.imageItem);
		itemTitleView.setText(itemName);
		itemDescriptionView.setText(itemDescription);
		Item itemObject = (Item) getIntent()
				.getSerializableExtra("item_object");
		if (new FavoritesDAL(getApplicationContext()).existsFavorit(itemObject)) {
			buttonFav.setBackgroundColor(Color.YELLOW);
		}
		if (itemObject.getImageOne() == null) {
			imageItem.setImageResource(R.drawable.unkown);
		} else {
			byte[] blob = itemObject.getImageOne();
			Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0, blob.length);
			if (bmp != null) {
				imageItem.setImageBitmap(bmp);
			} else {
				imageItem.setImageResource(R.drawable.unkown);
			}
		}

		if(!VaraibleGlobal.isHorsLigne()){
			
		buttonSite.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String url = itemWebsite;
				if (!url.startsWith("http://") || !url.startsWith("https://")) {
					url = "http://" + url;
				}
				Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
				startActivity(browse);
			}
		});
		}else{
//			 AlertDialog.Builder mesAlert = new AlertDialog.Builder(this);
//		        
//		        mesAlert.setTitle("Mon premier AlertDialog");
//		        mesAlert.show();
		}

		buttonAppler.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent callIntent = new Intent(Intent.ACTION_CALL);
				callIntent.setData(Uri.parse("tel:" + itemPhoneNumber));
				startActivity(callIntent);
			}
		});

		buttonMap.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				Intent itemMapScreenIntent = new Intent(
						getApplicationContext(), ItemMapActivity.class);
				ItemsGroupActivity parentActivity = (ItemsGroupActivity) getParent();
				itemMapScreenIntent.putExtra("lng_item", itemLng);
				itemMapScreenIntent.putExtra("lat_item", itemLat);
				parentActivity.startChildActivity("ItemMapActivity",
						itemMapScreenIntent);

			}
		});

		buttonFav.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Item item = new TourItemDAL(getApplicationContext())
						.getTourItem(Integer.parseInt(itemId));
				new FavoritesDAL(getApplicationContext()).addFavorit(item);
				if (new FavoritesDAL(getApplicationContext())
						.existsFavorit(item)) {
					buttonFav.setBackgroundColor(Color.YELLOW);
				} else {
					buttonFav.setBackgroundColor(Color.parseColor("#eeffffff"));
				}
			}
		});

		imageItem.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent itemImageGalleryScreenIntent = new Intent(
						getApplicationContext(), GalleryView.class);
				Item itemTemp = (Item) getIntent().getSerializableExtra(
						"item_object");
				Item item = new TourItemDAL(getApplicationContext())
						.getTourItem(itemTemp.getId());
				if (item.getImageOne() != null && item.getImageTwo() != null
						&& item.getImageThree() != null) {
					item.setListImages(new ArrayList<byte[]>());
					item.getListImages().add(item.getImageOne());
					item.getListImages().add(item.getImageTwo());
					item.getListImages().add(item.getImageThree());
					itemImageGalleryScreenIntent.putExtra("item_object", item);
					startActivity(itemImageGalleryScreenIntent);
				} else {
					System.out.println("Pas des images");
				}
			}
		});
	}
}
